package com.expressage.controller;


import com.expressage.po.CityAddress;
import com.expressage.po.SendMess;
import com.expressage.service.*;
import com.expressage.vo.AreaAddressVO;
import com.expressage.vo.RecipientVO;
import com.expressage.vo.SendMessVO;
import com.expressage.vo.StreetAddressVO;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * 发件人信息控制器
 */

@Controller
public class SendMessController {

    @Autowired
    SendMessService sendMessService;
    @Autowired
    CityAddressService cityAddressService;
    @Autowired
    AreaAddressService areaAddressService;
    @Autowired
    StreetAddressService streetAddressService;


    Logger logger = Logger.getLogger(SendMessController.class);

    @RequestMapping("/sendmesslist")
    public String findAll(Model model){
        logger.info("进入了发件人信息列表控制器");
        List<SendMessVO> list = sendMessService.findAll();
        model.addAttribute("list",list);
        logger.info(list);
        return "jspafter/sendmess";
    }
/*
    @RequestMapping("/recipientlistone")
    public String findByReid(int reid,Model model){
        logger.info("进入了单个收件人信息控制器");
        RecipientVO recipient = sendMessService.findByReid(reid);
        model.addAttribute("recipient",recipient);
        logger.info(recipient);
        return "jspafter/recipientlistone";
    }*/

    @RequestMapping("/toaddsendmess")
    public String toadd(Model model){
        List<SendMessVO> list =sendMessService.findAll();
        List<CityAddress> city = cityAddressService.findAll();
        List<AreaAddressVO> area = areaAddressService.findAll();
        List<StreetAddressVO> street = streetAddressService.findAll();
        model.addAttribute("street",street);
        model.addAttribute("area",area);
        model.addAttribute("city",city);
        model.addAttribute("list",list);
        return "jspafter/addsendmess";
    }

    @RequestMapping("addsendmess")
    public String addsendmess(SendMess sendMess){
        logger.info("进入了添加收件人信息控制器");
        Boolean flag = sendMessService.addSendMess(sendMess);
        logger.info(flag?"添加成功":"添加失败");
        return "redirect:sendmesslist.action";
    }

   

    @RequestMapping("/toupdatesendmess")
    public String updaterecipient(int fid,Model model){
        logger.info("进入了去修改控制器");
        SendMessVO sendMessVO = sendMessService.findByfid(fid);
        List<CityAddress> city = cityAddressService.findAll();
        List<AreaAddressVO> area = areaAddressService.findAll();
        List<StreetAddressVO> street = streetAddressService.findAll();
        model.addAttribute("street",street);
        model.addAttribute("area",area);
        model.addAttribute("city",city);
        model.addAttribute("sendmessVO",sendMessVO);
        logger.info(sendMessVO);
        return "/jspafter/updatesendmess";
    }

    @RequestMapping("/updatesendmess")
    public String update(SendMessVO sendMess){
        logger.info("进入了发件人信息修改控制器");
        Boolean flag = sendMessService.updateSendMess(sendMess);
        return "redirect:sendmesslist.action";
    }

     @RequestMapping("/deletesendmess")
    public String delete(int[] fid){
        logger.info("进入了删除收件人信息控制器");
        if(fid.length>0){
            boolean falg = sendMessService.deleteSendMess(fid);
            logger.info(falg?true:false);
        }
        return "redirect:sendmesslist.action";
    }

        
}
